package com.lei.baselibrary.base.common

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import java.util.*

/**
 * @ProjectName : MyMall
 * @Author : 雷金泷
 * @Time : 2021/2/24 18:28
 * @Description : 文件描述
 */
class AppManager private constructor(){
    private val activityStack:Stack<Activity> = Stack();
    companion object{
        val instance:AppManager by lazy { AppManager() }
    }
    //activity入栈
    fun addActivity(activity:Activity){
        activityStack.add(activity)
    }
    //activity出栈
    fun finishActivity(activity:Activity){
        activity.finish()
        activityStack.remove(activity)
    }
    //获取当前的栈顶
    fun currentActivity():Activity{
        return activityStack.lastElement()
    }
    //清理栈
    fun finishAllActivity(){
        for (activity in activityStack){
            activity.finish()
        }
        activityStack.clear()
    }
    //退出应用程序
    fun exitApp(context:Context){
        finishAllActivity()
        val appManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        appManager.killBackgroundProcesses(context.packageName)
        System.exit(0)
    }
}